<?php

    class FriendController extends Zend_Controller_Action
    {

        public function init()
        {
            /* Initialize action controller here */
        }

        public function indexAction()
        {
            $this->redirect("friend-list");
        }

        public function friendlistAction()
        {
            $x = new Zend_Auth_Storage_Session('User_Auth');
            $y = $x->read();

            if (!empty($y))
                $this->view->isLoggedIn = 'yes';
            else
                $this->view->isLoggedIn = 'no';

            $user_id = $this->_getParam('user_id', $y->user_id);
            if (!empty($user_id))
            {
                $this->view->isSameLoggedUser = 'no';
                if ($user_id != $x->read()->user_id)
                    $user_id = getEncryptedString($user_id, 'decode');
                else
                {
                    $this->view->isSameLoggedUser = 'yes';
                    //$this->view->isLoggedIn = 'yes';
                }
                $custom_model = new Application_Model_Custom();
                $this->view->user_id = $user_id;
                $friend_group_records = $custom_model->getFriendsAndGroups($user_id);
                //prd($friend_group_records);
                $commonModel = new Application_Model_Common();
                $friend_records = $commonModel->getAllRecordFromInnerJoin('friend_requests', 'users', 'users.user_id = friend_requests.user_sent_to', 'user_sent_from=' . $user_id . ' and user_active="1"');
                //prd($friend_records);
                foreach ($friend_records as $frndKey => $frndValue)
                {
                    $friendArr[] = $frndValue['user_id'];
                }
                
                
                $this->view->frndArr = $friendArr;
                //prd($friendArr);
                $this->view->enc_user_id = getEncryptedString($user_id);
                $this->view->friendGroupsAndFriends = $friend_group_records;
                //prd($friend_group_records);
                
                
            }
            else
            {
                $this->redirect("");
            }
            
        }

        public function creategroupAction()
        {
            $x = new Zend_Auth_Storage_Session('User_Auth');
            $y = $x->read();
            if (!empty($y) && $this->getRequest()->isPost())
            {
                $arr = $this->getRequest()->getPost();
                if (isset($arr["group_btn_submit"]))
                {
                    $model = new Application_Model_Common();
                    $user_id = $x->read()->user_id;
                    $group_name = trim($arr["group_name"]);

                    $is_exists = $model->is_exists(TABLE_FRIEND_GROUPS, "group_name = '$group_name' and created_by = $user_id");
                    if (!empty($is_exists))
                    {
                        $_SESSION["messageError"] = "Group <i>$group_name</i> already exists";
                        $this->redirect("friend-list");
                        die;
                    }

                    $data_array = array(
                        "group_name" => $group_name,
                        "created_by" => $user_id,
                        "from_ipaddress" => $_SERVER["REMOTE_ADDR"]
                    );
                    $model->insertRecordData($data_array, TABLE_FRIEND_GROUPS);
                    $_SESSION["messageSuccess"] = "New friend group created";
                    $this->redirect("friend-list");
                }
            }
            else
            {
                $this->redirect("");
            }
        }

        public function removefriendAction()
        {
            $x = new Zend_Auth_Storage_Session('User_Auth');
            $y = $x->read();
            if (!empty($y))
            {
                $friend_id = $this->getParam("friend_id");
                $group_id = $this->_getParam('group_id')=='others'?'0':$this->_getParam('group_id');
                $model = new Application_Model_Common();
                $user_id = $x->read()->user_id;
                    
                $model->deleteRecord("user_sent_from = $user_id and user_sent_to = $friend_id and group_id = $group_id ", TABLE_FRIEND_REQUESTS);

                $_SESSION["messageSuccess"] = "Friend removed";
                $this->redirect("friend-list");
            }
            else
            {
                $this->redirect("");
            }
        }

        public function deletegroupAction()
        {
            $x = new Zend_Auth_Storage_Session('User_Auth');
            $y = $x->read();
            if (!empty($y))
            {
                $group_id = $this->getParam("group_id");
                $model = new Application_Model_Common();
                $user_id = $x->read()->user_id;

                $model->updateRecordData(array("group_id" => "0"), "user_sent_from = $user_id and group_id = $group_id", TABLE_FRIEND_REQUESTS);
                $model->deleteRecord("group_id=$group_id", TABLE_FRIEND_GROUPS);

                $_SESSION["messageSuccess"] = "Friend group removed";
                $this->redirect("friend-list");
            }
            else
            {
                $this->redirect("");
            }
        }

        public function addAction()
        {
            $x = new Zend_Auth_Storage_Session('User_Auth');
            $y = $x->read();
            if ($this->getRequest()->getParam("user_id") && !empty($y))
            {
                $enc_user_id = $this->getParam("user_id");
                $request_send_from = $x->read()->user_id;
                $request_send_to = getEncryptedString($enc_user_id, "decode");
                $from_ipaddress = $_SERVER["REMOTE_ADDR"];

                //add request
                $data_array = array(
                    "user_sent_from" => $request_send_from,
                    "user_sent_to" => $request_send_to,
                    "from_ipaddress" => $from_ipaddress,
                );
                $model = new Application_Model_Common();
                $model->insertRecordData($data_array, TABLE_FRIEND_REQUESTS);
                $this->redirect("profile/publicprofile/$request_send_to");
            }
        }

        public function removeAction()
        {
            $x = new Zend_Auth_Storage_Session('User_Auth');
            $y = $x->read();
            if ($this->getRequest()->getParam("user_id") && !empty($y))
            {
                $enc_user_id = $this->getParam("user_id");
                $request_send_from = $x->read()->user_id;
                $request_send_to = getEncryptedString($enc_user_id, "decode");
                $whereCond = "user_sent_from = $request_send_from and user_sent_to = $request_send_to";

                $model = new Application_Model_Common();
                $model->deleteRecord($whereCond, TABLE_FRIEND_REQUESTS);
                $this->redirect("profile/publicprofile/$request_send_to");
            }
        }

        public function editgroupAction()
        {
            $x = new Zend_Auth_Storage_Session('User_Auth');
            $y = $x->read();
            if ($this->getRequest()->getParam("group_id") && !empty($y))
            {
                $group_id = $this->getParam("group_id");
                $whereCond = "group_id = $group_id and created_by = " . $x->read()->user_id;

                $model = new Application_Model_Common();
                $record = $model->getdata(TABLE_FRIEND_GROUPS, $whereCond);
                $this->view->group_id = $record["group_id"];
                $this->view->group_name = $record["group_name"];
            }

            if ($this->getRequest()->isPost() && !empty($y))
            {
                $arr = $this->getRequest()->getPost();

                $user_id = $x->read()->user_id;
                $group_id = trim($arr["group_id"]);
                $group_name = trim($arr["group_name"]);

                if (!empty($group_id) && !empty($group_name))
                {
                    $model->updateRecordData(array("group_name" => $group_name, "from_ipaddress" => $_SERVER["REMOTE_ADDR"]), "group_id = $group_id and created_by = $user_id", TABLE_FRIEND_GROUPS);
                    $_SESSION["messageSuccess"] = "Group name updated";
                }
                $this->redirect("friend-list");
            }
        }

    }

    